package com.example.freeengine.wallpaper.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import androidx.core.app.NotificationCompat;

import com.example.freeengine.R;
import com.example.freeengine.wallpaper.fragment.WallPaperViewModel;

import java.io.IOException;

/**
 * @author Terry Deng
 * not use
 */
public class MyWallPaperService extends Service {

    private static final String TAG = MyWallPaperService.class.getSimpleName();
    private static final String CHANNEL_ID = "FreeEngine_WallPaper_Set";
    WallpaperManager wallpaperManager;
    WallPaperViewModel wallPaperViewModel;

    public MyWallPaperService() {
    }

    public MyWallPaperService(WallPaperViewModel wallPaperViewModel) {
        this.wallPaperViewModel = wallPaperViewModel;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        wallpaperManager = WallpaperManager.getInstance(this);
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        String path = intent.getStringExtra("path");
        boolean showToast = intent.getBooleanExtra("showToast", true);
        Bitmap bitmap = BitmapFactory.decodeFile(path);

        try {
            wallpaperManager.setBitmap(bitmap);
            Log.i(TAG, "onStartCommand: " + "wallpaper set ok");
            if (showToast){
                Toast.makeText(this, "壁纸设置成功", Toast.LENGTH_SHORT).show();
            }
        } catch (IOException e) {
            e.printStackTrace();
            if (showToast){
                Toast.makeText(this, "壁纸设置失败", Toast.LENGTH_SHORT).show();
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "my foreground service";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

}